package form4;

import java.io.IOException;
import java.io.Reader;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;


import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.Model;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.form.Button;


//import org.apache.ibatis.io.Resources;
//import org.apache.ibatis.session.SqlSession;
//import org.apache.ibatis.session.SqlSessionFactory;
//import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import FMS.Form4;



import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

public class EditForm4 extends WebPage{
	
	//Reader reader = Resources.getResourceAsReader("sqlMapConfig.xml");
	//SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader);
	//SqlSession session = sqlMapper.openSession();
	
	Reader reader = Resources.getResourceAsReader("sqlMapConfig.xml");
	SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
	
	private Model liquidationFormNoIdModel = new Model();
	private Model nameOfRecipientofRCAModel = new Model();
	private Model perCashVoucherNoModel = new Model();
	private Model dateFiledModel = new Model();
	private Model amountReceivedModel = new Model();
	private Model totalAmountDisbursedModel = new Model();
	private Model amountToBeReturnedModel = new Model();
	private Model receiverForAmountRefundedModel = new Model();
	private Model dateAuditedModel = new Model();
	private Model dateReceiverReceivedAmountRefundedModel = new Model();
	private Model statusModel = new Model();
	private DropDownChoice status;
	private List choice = Arrays.asList(new String[] { "PENDING", "APPROVED" });
	
	
	public EditForm4() throws IOException, SQLException {
		
		Form form = new Form("editForm4");
		
		TextField liquidationFormNoId = new TextField("liquidationFormNoId", liquidationFormNoIdModel);
		liquidationFormNoId.setRequired(true);
		form.add(liquidationFormNoId);
		
		TextField nameOfRecipientofRCA = new TextField("nameOfRecipientofRCA", nameOfRecipientofRCAModel);
		nameOfRecipientofRCA.setRequired(true);
		form.add(nameOfRecipientofRCA);
		

		TextField perCashVoucherNo = new TextField("perCashVoucherNo", perCashVoucherNoModel);
		perCashVoucherNo.setRequired(true);
		form.add(perCashVoucherNo);
		

		TextField dateFiled = new TextField("dateFiled", dateFiledModel);
		dateFiled.setRequired(true);
		form.add(dateFiled);
		

		TextField amountReceived = new TextField("amountReceived", amountReceivedModel);
		amountReceived.setRequired(true);
		form.add(amountReceived);
		

		TextField totalAmountDisbursed = new TextField("totalAmountDisbursed", totalAmountDisbursedModel);
		totalAmountDisbursed.setRequired(true);
		form.add(totalAmountDisbursed);

		TextField amountToBeReturned = new TextField("amountToBeReturned", amountToBeReturnedModel);
		amountToBeReturned.setRequired(true);
		form.add(amountToBeReturned);
		
		TextField receiverForAmountRefunded = new TextField("receiverForAmountRefunded", receiverForAmountRefundedModel);
		receiverForAmountRefunded.setRequired(true);
		form.add(receiverForAmountRefunded);
		
		TextField dateAudited= new TextField("dateAudited", dateAuditedModel);
		dateAudited.setRequired(true);
		form.add(dateAudited);
		
		TextField dateReceiverReceivedAmountRefunded = new TextField("dateReceiverReceivedAmountRefunded", dateReceiverReceivedAmountRefundedModel);
		dateReceiverReceivedAmountRefunded.setRequired(true);
		form.add(dateReceiverReceivedAmountRefunded);
		
		status = new DropDownChoice("status", statusModel, choice);
		form.add(status);
		
		form.add(new Button("add"){
			
			private static final long serialVersionUID = 1L;
		
				public void onSubmit(){
				
					try {
				
						String liquidationFormNoId = (String)liquidationFormNoIdModel.getObject();
						String nameOfRecipientofRCA = (String)nameOfRecipientofRCAModel.getObject();
						String perCashVoucherNo = (String)perCashVoucherNoModel.getObject();
						String dateFiled = (String)dateFiledModel.getObject();
						String amountReceived = (String)amountReceivedModel.getObject();
						String totalAmountDisbursed = (String)totalAmountDisbursedModel.getObject();
						String amountToBeReturned = (String)amountToBeReturnedModel.getObject();
					    String receiverForAmountRefunded = (String)receiverForAmountRefundedModel.getObject();
	                    String dateAudited = (String)dateAuditedModel.getObject();
	                    String dateReceiverReceivedAmountRefunded = (String)dateReceiverReceivedAmountRefundedModel.getObject();
						String status = (String)statusModel.getObject();
	                    
						Form4 form4 = new Form4();
						
						form4.setLiquidationFormNoId(liquidationFormNoId);
						form4.setNameOfRecipientofRCA(nameOfRecipientofRCA);	
						form4.setPerCashVoucherNo(perCashVoucherNo);
						form4.setDateFiled(dateFiled);
						form4.setAmountReceived(amountReceived);			
						form4.setTotalAmountDisbursed(totalAmountDisbursed);
						form4.setAmountToBeReturned(amountToBeReturned);
						form4.setReceiverForAmountRefunded(receiverForAmountRefunded);
						form4.setDateAudited(dateAudited);
						form4.setDateReceiverReceivedAmountRefunded(dateReceiverReceivedAmountRefunded);
						form4.setStatus(status);
												
						
						sqlMap.insert("Form4.insert", form4);
						
						ViewForm4 page = new ViewForm4();
						setResponsePage(page);
						error("added");
					}
					catch (Exception exception){
					 	error("Fill up necessary fields");
				 	        exception.printStackTrace();}
				//}catch (IOException ioException) {
					//ioException.printStackTrace(); }
					
				}}); 
					
		//add(new FeedbackPanel("error"));
				
		add(form);
	
}
}